const VideoService = require("../services/videoService");

class VideoController {
  // 获取课程下的所有视频
  async getVideosByCourseId(ctx, next) {
    try {
      const { courseId } = ctx.params;
      const videos = await VideoService.getVideosByCourseId(courseId);

      if (!videos) {
        ctx.status = 404;
        ctx.body = { success: false, error: "找不到该课程下的视频" };
        return;
      }

      ctx.status = 200;
      ctx.body = { success: true, videos };
    } catch (error) {
      console.error("获取课程下的所有视频时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "获取课程下的所有视频时出错" };
    }
  }

  // 创建视频
  async createVideo(ctx, next) {
    try {
      const { courseId } = ctx.params;
      const { title, description, url } = ctx.request.body;
      const video = await VideoService.createVideo({
        courseId,
        title,
        description,
        url,
      });

      if (!video) {
        ctx.status = 404;
        ctx.body = { success: false, error: "找不到该课程" };
        return;
      }

      ctx.status = 201;
      ctx.body = { success: true, video };
    } catch (error) {
      console.error("创建视频时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "创建视频时出错" };
    }
  }

  // 获取单个视频
  async getVideoById(ctx, next) {
    try {
      const { courseId, videoId } = ctx.params;
      const video = await VideoService.getVideoById({ courseId, videoId });

      if (!video) {
        ctx.status = 404;
        ctx.body = { success: false, error: "找不到该视频" };
        return;
      }

      ctx.status = 200;
      ctx.body = { success: true, video };
    } catch (error) {
      console.error("获取单个视频时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "获取单个视频时出错" };
    }
  }

  // 更新视频
  async updateVideo(ctx, next) {
    try {
      const { courseId, videoId } = ctx.params;
      const { title, description, url } = ctx.request.body;
      const video = await VideoService.updateVideo({
        courseId,
        videoId,
        title,
        description,
        url,
      });

      if (!video) {
        ctx.status = 404;
        ctx.body = { success: false, error: "找不到该视频或课程" };
        return;
      }

      ctx.status = 200;
      ctx.body = { success: true, video };
    } catch (error) {
      console.error("更新视频时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "更新视频时出错" };
    }
  }

  // 删除视频
  async deleteVideo(ctx, next) {
    try {
      const { courseId, videoId } = ctx.params;
      const result = await VideoService.deleteVideo({ courseId, videoId });

      if (!result) {
        ctx.status = 404;
        ctx.body = { success: false, error: "找不到该视频或课程" };
        return;
      }

      ctx.status = 200;
      ctx.body = { success: true, message: "视频删除成功" };
    } catch (error) {
      console.error("删除视频时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "删除视频时出错" };
    }
  }

  // 批量删除视频
  async deleteVideos(ctx, next) {
    try {
      const { courseId } = ctx.params;
      const { videoIds } = ctx.request.body;
      const result = await VideoService.deleteVideos({ courseId, videoIds });

      if (!result) {
        ctx.status = 404;
        ctx.body = { success: false, error: "找不到指定视频或课程" };
        return;
      }

      ctx.status = 200;
      ctx.body = { success: true, message: "视频批量删除成功" };
    } catch (error) {
      console.error("批量删除视频时出错：", error);
      ctx.status = 500;
      ctx.body = { success: false, error: "批量删除视频时出错" };
    }
  }
}

module.exports = new VideoController();
